package com.sishu.njrtsdms.service.impl;


import com.sishu.njrtsdms.domain.core.GenericServiceImpl;
import com.sishu.njrtsdms.domain.dto.CollegeDTO;
import com.sishu.njrtsdms.domain.vo.CollegeVO;
import com.sishu.njrtsdms.exception.ServiceException;
import com.sishu.njrtsdms.jooq.codegen.tables.daos.CollegeDAO;
import com.sishu.njrtsdms.jooq.codegen.tables.pojos.College;
import com.sishu.njrtsdms.service.CollegeService;
import com.sishu.njrtsdms.utils.MyBeanUtils;

import org.jooq.types.ULong;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollegeServiceImpl extends GenericServiceImpl<College, org.jooq.types.ULong>
        implements CollegeService {

    private final CollegeDAO collegeDAO;


    public CollegeServiceImpl(CollegeDAO dao) {
        super(dao);
        this.collegeDAO = dao;
    }

    @Override
    public List<CollegeVO> listCollege(CollegeDTO collegeDTO) {
        return collegeDAO.listCollege(collegeDTO);
    }

    @Override
    public CollegeVO queryOneCollegeByCollegeId(Long collegeId) {
        return collegeDAO.queryOneCollegeByCollegeId(collegeId);
    }

    @Override
    public void addCollege(CollegeDTO collegeDTO) {
        var college = MyBeanUtils.copyBean(collegeDTO, College.class);
        collegeDAO.insertSelective(college);
    }

    @Override
    public void updateCollege(ULong collegeId, CollegeDTO collegeDTO) {
        var collegeDb = collegeDAO.fetchOneByCollegeId(collegeId);
        if (collegeDb == null || collegeDb.getIsDeleted())
            throw new ServiceException("学院不存在或已被删除");
        var college = MyBeanUtils.copyBean(collegeDTO, College.class);
        college.setCollegeId(collegeId);
        collegeDAO.updateSelective(college);
    }
}